#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>

#define Conver(Addrptr) ((struct sockaddr *)Addrptr)

const static int DefaultSocfd = -1;
const static int Backlog = 5;

namespace NetWork
{

    class Socket
    {
    public:
        virtual ~Socket(){};
        virtual void CreateSocketOrDie() = 0;
        virtual void BindSocketOrDie(uint16_t port) = 0;
        virtual void ListenSocketOrDie(int backlog) = 0;
        virtual Socket *AcceptSocket(std::string *peer, uint16_t *peerport) = 0;
        virtual bool ConnectSocket(std::string &serverip, int16_t serverport) = 0;
        virtual int GetSockFd() = 0;
        virtual void SetSockFd(int sockfd) = 0;
        virtual void CloseSockFd() = 0;

    public:
        void BuildListenSocket(uint16_t port, int backlog)
        {
            CreateSocketOrDie();
            BindSocketOrDie(port);
            ListenSocketOrDie(backlog);
        }
        void BuildConnectSocket(std::string &serverip, int16_t serverport)
        {
            CreateSocketOrDie();
            ConnectSocket(serverip, serverport);
        }
        void BuildNormalSocket(int sockfd)
        {
            SetSockFd(sockfd);
        }
    };

    enum
    {
        CreateERR = 1,
        BindERR,
        ListenERR
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int socket = DefaultSocfd) : _sockfd(socket)
        {
        }
        ~TcpSocket()
        {
        }
        void CloseSockFd()
        {
            ::close(_sockfd);
        }

        void CreateSocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                exit(CreateERR);
            }
        }
        void BindSocketOrDie(uint16_t port) override
        {
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = INADDR_ANY;

            int n = ::bind(_sockfd, Conver(&local), sizeof(local));
            if (n < 0)
            {
                exit(BindERR);
            }
        }
        void ListenSocketOrDie(int backlog) override
        {
            int n = ::listen(_sockfd, backlog);
            if (n < 0)
            {
                exit(ListenERR);
            }
        }
        Socket *AcceptSocket(std::string *peer, uint16_t *peerport) override
        {
            struct sockaddr_in Peer;
            socklen_t len = sizeof(Peer);
            int newsockfd = ::accept(_sockfd, Conver(&Peer), &len);
            if (newsockfd < 0)
                return nullptr;
            Socket *s = new TcpSocket(newsockfd);
            return s;
        }

        bool ConnectSocket(std::string &serverip, int16_t serverport) override
        {
            struct sockaddr_in Server;
            memset(&Server, 0, sizeof(Server));
            Server.sin_family = AF_INET;
            Server.sin_port = htons(serverport);
            Server.sin_addr.s_addr = inet_addr(serverip.c_str());
            int n = ::connect(_sockfd, Conver(&Server), sizeof(Server));
            if (n == 0)
                return true;
            else
                return false;
        }
        int GetSockFd() override
        {
            return _sockfd;
        }
        void SetSockFd(int sockfd) override
        {
            _sockfd = sockfd;
        }

    private:
        int _sockfd;
    };
}
